package com.aaa.set.service.impl;

import com.aaa.set.dao.UpdateMemberPasswordDao;
import com.aaa.set.service.UpdateMemberPasswordService;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 荆豪峰
 * @version 1.0
 * #Date: 2023/3/9 11:08
 */
@Service
public class UpdateMemberPasswordServiceImpl implements UpdateMemberPasswordService {
    @Autowired
    private UpdateMemberPasswordDao updateMemberPasswordDao;
    /**
     * #Description 用户修改密码输入旧密码
     * @param oldPassword
 * @param request
     * @return com.aaa.vo.Result
     * @author 荆豪峰
     * #Date 2023/3/9
     */
    @Override
    public Result findPassword(String oldPassword, HttpServletRequest request) {
        String token = request.getParameter("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = (String) tokenChaim.get("username");
        String  oldPw= updateMemberPasswordDao.findoldPassword(username);
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        if (passwordEncoder.matches(oldPassword,oldPw)){
            return new Result(2000,"该密码可用");
        }
        return new Result(5000,"该密码不可用");
    }
    /**
     * #Description 用户修改密码
     * @param password
     * @param request
     * @return com.aaa.vo.Result
     * @author 荆豪峰
     * #Date 2023/3/9
     */
    @Override
    public Result UpdatePassword(String password, HttpServletRequest request) {
        String token = request.getParameter("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = (String) tokenChaim.get("username");
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(password);
         int i =  updateMemberPasswordDao.updatePassword(encode,username);
        return new Result(2000,"修改成功");
    }
}
